package test.dao;

import main.entity.eDish;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;




import java.util.ArrayList;
import java.util.List;

@Repository
public class HibernateDish extends Dao
{
	public eDish add(eDish dish) 
	{
		dish.setId( (Integer)sessionFactory.getCurrentSession().save( dish ) );
	
		return dish;
	}
	
	public void delete(int id)  
	{
		Query query;
		query = sessionFactory.getCurrentSession().createQuery("delete from eDish where id = " + id);
		query.executeUpdate();
	}

	public void DeleteAll()  
	{
		Query query;
		query = sessionFactory.getCurrentSession().createQuery("delete from eDish");
		query.executeUpdate();
	}

	@SuppressWarnings("unchecked")
	public eDish Read(eDish dish)  
	{
		int id = dish.getId();
		
		List<eDish> list = sessionFactory.getCurrentSession().createQuery( "from eDish where id = "+id ).list();
		
		if (list.isEmpty())
		{
			return dish;
		}
		
		return list.get(0);
	}
	
	@SuppressWarnings("unchecked")
	public eDish Read(int id)  
	{
		eDish dish = new eDish();
		
		List<eDish> list = sessionFactory.getCurrentSession().createQuery("from eDish where id = "+id).list();
		
		if (list.isEmpty())
		{
			return dish;
		}
		
		return list.get(0);
	}

	@SuppressWarnings("unchecked")
	public ArrayList<eDish> ReadAll()  
	{
		return (ArrayList<eDish>) sessionFactory.getCurrentSession().createQuery("from eDish").list();
	}
	
	public ArrayList<eDish> getDishesByIds( ArrayList<Integer> idList)
	{
		ArrayList<eDish> list = new ArrayList<eDish>();
		
		for (int id : idList)
		{
			list.add( (eDish)sessionFactory.getCurrentSession().get(eDish.class, id) );
		}
		
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public ArrayList<eDish> getDishesByType(int id)  
	{
		return (ArrayList<eDish>) sessionFactory.getCurrentSession().createQuery("from eDish where TYPE = "+id).list();
	}

}
